package com.design.classroommanagement.student

import com.alibaba.android.arouter.facade.annotation.Route
import com.design.classroommanagement.Pages
import com.design.classroommanagement.api.Classroom
import com.design.classroommanagement.api.dao.ClassRoomDao
import com.design.classroommanagement.databinding.ActivityTeacherBookmarkBinding
import com.design.classroommanagement.manager.UserAccountManager
import com.laughing.lib.base.BaseFragmentNoVM
import com.laughing.lib.utils.clickWithTrigger
import com.laughing.lib.utils.runInBg
import com.laughing.lib.utils.runInMain
import com.laughing.lib.utils.saveGet

@Route(path = Pages.StudentBookmarkProgressFragment)
class StudentBookmarkProgressFragment : BaseFragmentNoVM<ActivityTeacherBookmarkBinding>() {
    private var classroom: Classroom? = null

    override fun initCompleted() {
        super.initCompleted()
        binding.apply {
            submitBtn.clickWithTrigger {
                submit()
            }
            searchBtn.clickWithTrigger {
                search()
            }
        }
    }

    fun search() {
        val key = binding.classnameEt.text.trim().toString()
        runInBg {
            var result = ClassRoomDao.search(key)
            classroom = result.saveGet(0)
            runInMain { render() }
        }
    }

    fun render() {
        binding.apply {
            bookmarkEt.setText(classroom?.bookmarkId)
            progressEt.setText(classroom?.progress)
            classnameEt.setText(classroom?.name)
        }
    }

    fun submit() {
        val bookmark = classroom?.apply {
            bookmarkId = binding.bookmarkEt.text.toString()
            name = binding.classnameEt.text.toString()
            progress = binding.progressEt.text.toString()
        } ?: return

        runInBg {
            ClassRoomDao.update(bookmark)
            finish()
        }
    }

}